package m.jclecai.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import m.jclecai.com.R;

import java.text.DecimalFormat;
import java.util.List;

import m.jclecai.com.bean.ZqJQSItem;
import m.jclecai.com.listener.NotifyTextViewSetChanged;
import m.jclecai.com.utils.Utils;
import m.jclecai.com.utils.http.FastOk;

/**
 * 总进球
 * Created by hp on 2017/3/23.
 */

public class ExpandableZJQAdapter  extends BaseExpandableListAdapter {
    private Context context;
    private List<String> groups;

    List<List<ZqJQSItem>> lists;
    private NotifyTextViewSetChanged changed;


    public ExpandableZJQAdapter(){

    }
    public ExpandableZJQAdapter(Context context, List<String> groups,
                                List<List<ZqJQSItem>> lists, NotifyTextViewSetChanged changed ){
        this.context=context;
        this.groups = groups;
        this.lists = lists;
        this.changed = changed;
    }
    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return lists.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return lists.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      GroupViewHolder groupViewHolder;
        if(convertView==null){
            convertView=View.inflate(context, R.layout.item_parent,null);
            groupViewHolder=new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.list_item_parent_title.setText(groups.get(groupPosition));

        if(isExpanded){
            groupViewHolder.list_item_parent_icon.setImageResource(R.drawable.cz);
        }else {
            groupViewHolder.list_item_parent_icon.setImageResource(R.drawable.d0);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChlidViewHolder chlidViewHolder;
        if(convertView==null){
            convertView=View.inflate(context,R.layout.item_zq_zjq,null);
            chlidViewHolder=new ChlidViewHolder(convertView);
            convertView.setTag(chlidViewHolder);
        }else {
            chlidViewHolder= (ChlidViewHolder) convertView.getTag();
        }
        final ZqJQSItem item = (ZqJQSItem) getChild(groupPosition,childPosition);
        String[] codes = item.getStrings();

        final List<String[]> rqsflists = FastOk.getList(codes[16], String[].class);
        DecimalFormat df = new DecimalFormat("#.00");

        chlidViewHolder.away_team_name.setText(Html.fromHtml(codes[4]));
        chlidViewHolder.home_team_name.setText(Html.fromHtml(codes[3]));
        chlidViewHolder.item_name.setText(codes[2]);//世界杯预选赛
        chlidViewHolder.item_time.setText(codes[7].substring(5, 16)+"\n"+codes[1]);

        chlidViewHolder.odd_zero.setText("0球\n"+df.format(Double.parseDouble(rqsflists.get(2)[0])));
        chlidViewHolder.odd_one.setText("1球\n"+df.format(Double.parseDouble(rqsflists.get(2)[1])));
        chlidViewHolder.odd_two.setText("2球\n"+df.format(Double.parseDouble(rqsflists.get(2)[2])));

        chlidViewHolder.odd_three.setText("3球\n"+df.format(Double.parseDouble(rqsflists.get(2)[3])));
        chlidViewHolder.odd_four.setText("4球\n"+df.format(Double.parseDouble(rqsflists.get(2)[4])));
        chlidViewHolder.odd_five.setText("5+球\n"+df.format(Double.parseDouble(rqsflists.get(2)[5])));
        chlidViewHolder.odd_six.setText("6球\n"+df.format(Double.parseDouble(rqsflists.get(2)[6])));
        chlidViewHolder.odd_seven.setText("7球\n"+df.format(Double.parseDouble(rqsflists.get(2)[7])));


        chlidViewHolder.odd_zero.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (item.isSelected0()) {
                    item.getList().remove("0");
                    item.getCodeList().remove("0球");
                    item.getOdd_sp().remove(rqsflists.get(2)[0]);
                    item.setSelected0(false);
                } else {
                    item.getList().add("0");
                    item.getCodeList().add("0球");
                    item.setSelected0(true);
                    item.getOdd_sp().add(rqsflists.get(2)[0]);
                }
                notifyDataSetChanged();
                isAdd(item);
            }
        });
        chlidViewHolder.odd_one.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (item.isSelected1()) {
                    item.getList().remove("1");
                    item.getCodeList().remove("1球");
                    item.setSelected1(false);
                    item.getOdd_sp().remove(rqsflists.get(2)[1]);
                } else {
                    item.getList().add("1");
                    item.getCodeList().add("1球");
                    item.setSelected1(true);
                    item.getOdd_sp().add(rqsflists.get(2)[1]);
                }
                notifyDataSetChanged();
                isAdd(item);
            }
        });
        chlidViewHolder.odd_two.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (item.isSelected2()) {
                    item.getList().remove("2");
                    item.getCodeList().remove("2球");
                    item.setSelected2(false);
                    item.getOdd_sp().remove(rqsflists.get(2)[2]);
                } else {
                    item.getList().add("2");
                    item.getCodeList().add("2球");
                    item.setSelected2(true);
                    item.getOdd_sp().add(rqsflists.get(2)[2]);
                }
                notifyDataSetChanged();
                isAdd(item);
            }
        });
        chlidViewHolder.odd_three.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (item.isSelected3()) {
                    item.getList().remove("3");
                    item.getCodeList().remove("3球");
                    item.setSelected3(false);
                    item.getOdd_sp().remove(rqsflists.get(2)[3]);
                } else {
                    item.getList().add("3");
                    item.getCodeList().add("3球");
                    item.setSelected3(true);
                    item.getOdd_sp().add(rqsflists.get(2)[3]);
                }
                notifyDataSetChanged();
                isAdd(item);
            }
        });
        chlidViewHolder.odd_four.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (item.isSelected4()) {
                    item.getList().remove("4");
                    item.getCodeList().remove("4球");
                    item.setSelected4(false);
                    item.getOdd_sp().remove(rqsflists.get(2)[4]);
                } else {
                    item.getList().add("4");
                    item.getCodeList().add("4球");
                    item.setSelected4(true);
                    item.getOdd_sp().add(rqsflists.get(2)[4]);
                }
                notifyDataSetChanged();
                isAdd(item);
            }
        });
        chlidViewHolder.odd_five.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (item.isSelected5()) {
                    item.getList().remove("5");
                    item.getCodeList().remove("5球");
                    item.setSelected5(false);
                    item.getOdd_sp().remove(rqsflists.get(2)[5]);
                } else {
                    item.getList().add("5");
                    item.getCodeList().add("5球");
                    item.setSelected5(true);
                    item.getOdd_sp().add(rqsflists.get(2)[5]);
                }
                notifyDataSetChanged();
                isAdd(item);
            }
        });
        chlidViewHolder.odd_six.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (item.isSelected6()) {
                    item.getList().remove("6");
                    item.getCodeList().remove("6球");
                    item.setSelected6(false);
                    item.getOdd_sp().remove(rqsflists.get(2)[6]);
                } else {
                    item.getList().add("6");
                    item.getCodeList().add("6球");
                    item.setSelected6(true);
                    item.getOdd_sp().add(rqsflists.get(2)[6]);
                }
                notifyDataSetChanged();
                isAdd(item);
            }
        });
        chlidViewHolder.odd_seven.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (item.isSelected7()) {
                    item.getList().remove("7");
                    item.getCodeList().remove("7+球");
                    item.setSelected7(false);
                    item.getOdd_sp().remove(rqsflists.get(2)[7]);
                } else {
                    item.getList().add("7");
                    item.getCodeList().add("7+球");
                    item.setSelected7(true);
                    item.getOdd_sp().add(rqsflists.get(2)[7]);
                }
                notifyDataSetChanged();
                isAdd(item);
            }
        });

        if (item.isSelected0()) {
            chlidViewHolder.odd_zero.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
            chlidViewHolder.odd_zero.setTextColor(Color.WHITE);
        } else {
            chlidViewHolder.odd_zero.setTextColor(Color.BLACK);
            chlidViewHolder.odd_zero.setBackgroundResource(R.drawable.odd_normal);
        }
        if (item.isSelected1()) {
            chlidViewHolder.odd_one.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
            chlidViewHolder.odd_one.setTextColor(Color.WHITE);
        } else {
            chlidViewHolder.odd_one.setTextColor(Color.BLACK);
            chlidViewHolder.odd_one.setBackgroundResource(R.drawable.odd_normal);
        }
        if (item.isSelected2()) {
            chlidViewHolder.odd_two.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
            chlidViewHolder.odd_two.setTextColor(Color.WHITE);
        } else {
            chlidViewHolder.odd_two.setTextColor(Color.BLACK);
            chlidViewHolder.odd_two.setBackgroundResource(R.drawable.odd_normal);
        }
        if (item.isSelected3()) {
            chlidViewHolder.odd_three.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
            chlidViewHolder.odd_three.setTextColor(Color.WHITE);
        } else {
            chlidViewHolder.odd_three.setTextColor(Color.BLACK);
            chlidViewHolder.odd_three.setBackgroundResource(R.drawable.odd_normal);
        }
        if (item.isSelected4()) {
            chlidViewHolder.odd_four.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
            chlidViewHolder.odd_four.setTextColor(Color.WHITE);
        } else {
            chlidViewHolder.odd_four.setTextColor(Color.BLACK);
            chlidViewHolder.odd_four.setBackgroundResource(R.drawable.odd_normal);
        }
        if (item.isSelected5()) {
            chlidViewHolder.odd_five.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
            chlidViewHolder.odd_five.setTextColor(Color.WHITE);
        } else {
            chlidViewHolder.odd_five.setTextColor(Color.BLACK);
            chlidViewHolder.odd_five.setBackgroundResource(R.drawable.odd_normal);
        }
        if (item.isSelected6()) {
            chlidViewHolder.odd_six.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
            chlidViewHolder.odd_six.setTextColor(Color.WHITE);
        } else {
            chlidViewHolder.odd_six.setTextColor(Color.BLACK);
            chlidViewHolder.odd_six.setBackgroundResource(R.drawable.odd_normal);
        }
        if (item.isSelected7()) {
            chlidViewHolder.odd_seven.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
            chlidViewHolder.odd_seven.setTextColor(Color.WHITE);
        } else {
            chlidViewHolder.odd_seven.setTextColor(Color.BLACK);
            chlidViewHolder.odd_seven.setBackgroundResource(R.drawable.odd_normal);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    static class GroupViewHolder{
        private final TextView list_item_parent_title;
        private final ImageView list_item_parent_icon;

        public GroupViewHolder(View view){
            list_item_parent_title = (TextView) view.findViewById(R.id.list_item_parent_title);
            list_item_parent_icon = (ImageView) view.findViewById(R.id.list_item_parent_icon);
        }
    }
    static class ChlidViewHolder{
        private final TextView item_name;
        private final TextView item_time;
        private final TextView home_team_name;
        private final TextView away_team_name;
        private final TextView odd_zero;
        private final TextView odd_one;
        private final TextView odd_two;
        private final TextView odd_three;
        private final TextView odd_four;
        private final TextView odd_six;
        private final TextView odd_seven;
        private final TextView odd_five;

        public ChlidViewHolder(View view){
            item_name = (TextView) view.findViewById(R.id.item_name);
            item_time = (TextView) view.findViewById(R.id.item_time);
            home_team_name = (TextView) view.findViewById(R.id.home_team_name);
            away_team_name = (TextView) view.findViewById(R.id.away_team_name);
            odd_zero = (TextView) view.findViewById(R.id.odd_zero);
            odd_one = (TextView) view.findViewById(R.id.odd_one);
            odd_two = (TextView) view.findViewById(R.id.odd_two);
            odd_three = (TextView) view.findViewById(R.id.odd_three);
            odd_four = (TextView) view.findViewById(R.id.odd_four);
            odd_six = (TextView) view.findViewById(R.id.odd_six);
            odd_seven = (TextView) view.findViewById(R.id.odd_seven);
            odd_five = (TextView) view.findViewById(R.id.odd_five);
        }
    }

    private void isAdd(ZqJQSItem item) {
        // TODO Auto-generated method stub
        if (item.isSelected0() || item.isSelected1() || item.isSelected2()
                || item.isSelected3() || item.isSelected4()
                || item.isSelected5() || item.isSelected6()
                || item.isSelected7()) {
            if (!Utils.ZQ_ZJQ_ITEM_LIST.contains(item)) {
                Utils.ZQ_ZJQ_ITEM_LIST.add(item);
            }
        } else {
            if (Utils.ZQ_ZJQ_ITEM_LIST.contains(item)) {
                Utils.ZQ_ZJQ_ITEM_LIST.remove(item);
            }
        }
        changed.onClickSetChangedText();
    }
}
